//
//  GetJsonData.m
//  LoanTeam15
//
//  Created by gynjobs on 2017/8/18.
//  Copyright © 2017年 cangming. All rights reserved.
//

#import "GetJsonData.h"

@interface GetJsonData()
-(instancetype)init;
@end

@implementation GetJsonData


+(id)standardGetInstance{
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[GetJsonData alloc]init];
    });
    return instance;
}

-(instancetype) init{
    self = [super init];
    
    if(self){
        
        
    }
    
    return self;
}


-(id)getJsonData:(NSString*)name{
    
    NSError * error = nil;
    NSString * jsonPath = [[NSBundle mainBundle] pathForResource:name ofType:@"json"];
    NSData * data = [[NSData alloc] initWithContentsOfFile:jsonPath];
    
    NSDictionary * jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    
    if(error){
        
        GYNLog(@" json error ");
        return nil;
        
    }else{
        
        NSMutableDictionary * fdic = [NSMutableDictionary dictionaryWithDictionary:[jsonDic valueForKey:@"details"]];
        return fdic;
        
    }
    
}

#pragma mark
#pragma mark api

// api

-(NSString*) getLoanJsonItemData:(NSString*)key withId:(NSInteger) lid{
    
    NSString * fileName = @"loan";
    
    return [self getJsonItemData:fileName withKey:key withId:lid];
    
}

-(NSString*) getCityJsonItemData:(NSString*)key withId:(NSInteger) lid{
    
    NSString * fileName = @"city3";
    return [self getJsonItemData:fileName withKey:key withId:lid];
    
}



-(NSString *) getJsonItemData:(NSString *)fileName withKey:(NSString*)key withId:(NSInteger)lid{
    
    // check loan
    
    NSString * itemContent = @"";
    
    
    NSMutableDictionary * dic = [self getJsonData:fileName];
    
    if([fileName isEqualToString:@"loan"]){
        
        itemContent = [self getLoanJsonItem:dic withKey:key withId:lid];
        
    }else if ([fileName isEqualToString:@"city3"]){
        
        itemContent = [self getCityJsonItem:dic withKey:key withId:lid];
    }else{
        
        
    }
    
    return itemContent;
    
}

-(NSString * )getLoanJsonItem:(id)source withKey:(NSString*)key  withId:(NSInteger)lid{
    
    NSString * itemContent = @"";
    NSMutableArray * array = [source valueForKey:key];
    
    
    for (NSInteger i = 0 ;i < [array count] ; i ++ ) {
        
        NSMutableDictionary *item = [array objectAtIndex:i];
        if([[item valueForKey:@"key"] integerValue] == lid){
            
            itemContent = [item valueForKey:@"name"];
            
            break;
            
        }
        
    }
    
    return itemContent;
    
    
}

-(NSString *)getCityJsonItem:(id) source withKey:(NSString *) key withId:(NSInteger)lid{
    
    NSString * itemContent = @"";
    
    NSMutableArray * array = [source valueForKey:@"all"];
    
    NSInteger pre2 = lid / 10000;
    
    for (NSInteger i = 0; i< [array count] ; i++) {
        
        NSDictionary * dic = [array objectAtIndex:i];
        
        NSInteger zone_id = [[dic valueForKey:@"zone_id"] integerValue];
        
        if(zone_id / 10000 == pre2){
            
            NSString * cityName = [dic valueForKey:@"zone_name"];
            
            NSMutableArray * subCitys = [dic valueForKey:@"subs"];
            
            
            // no sub citys
            
            if(!subCitys){
                
                itemContent = cityName;
                break;
            }
            
            
            for (NSInteger j = 0; j < [subCitys count]; j ++ ) {
                
                
                NSMutableDictionary * subDic = [subCitys objectAtIndex:j];
                
                if([[subDic valueForKey:@"zone_id"] integerValue] == lid){
                    
                    
                    NSString *subCityName = [subDic valueForKey:@"zone_name"];
                    
                    itemContent = [NSString stringWithFormat:@"%@ %@", cityName,subCityName];
                    
                    break;
                    
                }
                
            }
            
        }
        
    }
    
    return itemContent;
    
}


// base json

-(id)getJsonBaseData:(NSString*)fileName withKey:(NSString*)key{
    
    NSMutableDictionary * dic = [self getJsonData:fileName];
    return [dic valueForKey:key];
    
}


-(id)getJsonBaseItemData:(NSString *) subKey{
    
    NSDictionary * dic = [self getJsonBaseData:@"base" withKey:@"data"];
    return [dic valueForKey:subKey];
    
}


@end
